<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI规则库管理 - BIM智能安全分析平台</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
        
        body {
            font-family: 'Inter', sans-serif;
        }
        
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .glass-card {
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        .hover-lift {
            transition: all 0.3s ease;
        }
        
        .hover-lift:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .rule-card {
            transition: all 0.3s ease;
            border-left: 4px solid transparent;
        }
        
        .rule-card:hover {
            border-left-color: #3b82f6;
            transform: translateX(4px);
        }
        
        .severity-critical {
            border-left-color: #ef4444;
            background: linear-gradient(to right, rgba(239, 68, 68, 0.1), transparent);
        }
        
        .severity-medium {
            border-left-color: #f59e0b;
            background: linear-gradient(to right, rgba(245, 158, 11, 0.1), transparent);
        }
        
        .severity-minor {
            border-left-color: #10b981;
            background: linear-gradient(to right, rgba(16, 185, 129, 0.1), transparent);
        }
        
        .floating-animation {
            animation: float 6s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
    </style>
</head>
<body class="bg-gradient-to-br from-slate-50 via-blue-50 to-indigo-100 min-h-screen">
    <div id="app">
        <!-- 动态背景粒子 -->
        <div class="fixed inset-0 overflow-hidden pointer-events-none">
            <div class="absolute -top-40 -right-40 w-80 h-80 bg-purple-300 rounded-full mix-blend-multiply filter blur-xl opacity-70 animate-pulse"></div>
            <div class="absolute -bottom-40 -left-40 w-80 h-80 bg-yellow-300 rounded-full mix-blend-multiply filter blur-xl opacity-70 animate-pulse" style="animation-delay: 2s;"></div>
            <div class="absolute top-1/2 left-1/2 w-80 h-80 bg-pink-300 rounded-full mix-blend-multiply filter blur-xl opacity-70 animate-pulse" style="animation-delay: 4s;"></div>
        </div>

        <!-- 头部导航 -->
        <header class="relative bg-white/80 backdrop-blur-md shadow-lg border-b border-white/20">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
                <div class="flex items-center justify-between h-20">
                    <div class="flex items-center space-x-4">
                        <div class="relative">
                            <div class="absolute inset-0 bg-gradient-to-r from-green-600 to-blue-600 rounded-xl blur opacity-75"></div>
                            <div class="relative bg-gradient-to-r from-green-600 to-blue-600 p-3 rounded-xl">
                                <i class="fas fa-robot text-2xl text-white"></i>
                            </div>
                        </div>
                        <div>
                            <h1 class="text-3xl font-bold bg-gradient-to-r from-gray-900 to-gray-700 bg-clip-text text-transparent">
                                AI规则库管理
                            </h1>
                            <p class="text-sm text-gray-600">智能规则 · 一键构建 · 实时预览</p>
                        </div>
                    </div>
                    
                    <nav class="flex space-x-1">
                        <button @click="currentTab = 'build'" 
                                :class="currentTab === 'build' ? 'bg-gradient-to-r from-green-600 to-blue-600 text-white shadow-lg' : 'text-gray-600 hover:text-gray-900'"
                                class="px-6 py-2.5 rounded-xl font-medium transition-all duration-300 hover-lift">
                            <i class="fas fa-plus-circle mr-2"></i>构建规则
                        </button>
                        <button @click="currentTab = 'history'" 
                                :class="currentTab === 'history' ? 'bg-gradient-to-r from-blue-600 to-purple-600 text-white shadow-lg' : 'text-gray-600 hover:text-gray-900'"
                                class="px-6 py-2.5 rounded-xl font-medium transition-all duration-300 hover-lift">
                            <i class="fas fa-history mr-2"></i>历史记录
                        </button>
                        <button @click="currentTab = 'preview'" 
                                :class="currentTab === 'preview' ? 'bg-gradient-to-r from-purple-600 to-pink-600 text-white shadow-lg' : 'text-gray-600 hover:text-gray-900'"
                                class="px-6 py-2.5 rounded-xl font-medium transition-all duration-300 hover-lift">
                            <i class="fas fa-eye mr-2"></i>规则预览
                        </button>
                    </nav>
                </div>
            </div>
        </header>

        <!-- 主内容区域 -->
        <main class="relative max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
            <!-- 构建规则页面 -->
            <div v-if="currentTab === 'build'" class="space-y-8">
                <div class="grid lg:grid-cols-3 gap-8">
                    <!-- 规则构建器 -->
                    <div class="lg:col-span-2">
                        <div class="bg-white/90 backdrop-blur-sm rounded-2xl shadow-2xl p-8 hover-lift">
                            <div class="flex items-center mb-6">
                                <div class="bg-gradient-to-r from-green-600 to-blue-600 p-3 rounded-xl text-white">
                                    <i class="fas fa-magic text-xl"></i>
                                </div>
                                <h2 class="text-2xl font-bold text-gray-900 ml-4">AI规则构建器</h2>
                            </div>
                            
                            <div class="space-y-6">
                                <div>
                                    <label class="block text-sm font-semibold text-gray-700 mb-3">建筑类型选择</label>
                                    <div class="grid grid-cols-2 sm:grid-cols-3 gap-3">
                                        <button v-for="type in buildingTypes" :key="type.value"
                                                @click="selectedBuildingType = type.value"
                                                :class="selectedBuildingType === type.value ? 'bg-gradient-to-r from-green-600 to-blue-600 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200'"
                                                class="p-4 rounded-xl text-center transition-all duration-300 hover-lift">
                                            <i :class="type.icon" class="text-2xl mb-2 block"></i>
                                            <span class="text-sm font-medium">{{ type.label }}</span>
                                        </button>
                                    </div>
                                </div>
                                
                                <div>
                                    <label class="block text-sm font-semibold text-gray-700 mb-3">项目上下文（可选）</label>
                                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                                        <div>
                                            <label class="block text-xs text-gray-600 mb-1">建筑高度 (m)</label>
                                            <input v-model="projectContext.building_height" type="number" 
                                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-transparent"
                                                   placeholder="例如: 24">
                                        </div>
                                        <div>
                                            <label class="block text-xs text-gray-600 mb-1">总建筑面积 (m²)</label>
                                            <input v-model="projectContext.total_area" type="number" 
                                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-transparent"
                                                   placeholder="例如: 5000">
                                        </div>
                                        <div class="sm:col-span-2">
                                            <label class="block text-xs text-gray-600 mb-1">使用性质</label>
                                            <select v-model="projectContext.occupancy" 
                                                    class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-transparent">
                                                <option value="standard">标准</option>
                                                <option value="high_density">高密度</option>
                                                <option value="public">公共</option>
                                                <option value="special">特殊</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                                    <button @click="buildAIRules" 
                                            :disabled="!selectedBuildingType || isBuilding"
                                            class="w-full bg-gradient-to-r from-green-600 to-blue-600 text-white py-4 rounded-xl font-semibold hover:from-green-700 hover:to-blue-700 disabled:from-gray-400 disabled:to-gray-500 disabled:cursor-not-allowed transition-all duration-300 hover-lift shadow-lg">
                                        <span v-if="!isBuilding" class="flex items-center justify-center">
                                            <i class="fas fa-robot mr-2"></i>构建AI规则库
                                        </span>
                                        <span v-else class="flex items-center justify-center">
                                            <i class="fas fa-spinner fa-spin mr-2"></i>AI构建中...
                                        </span>
                                    </button>
                                    
                                    <button @click="loadDemoRules" 
                                            :disabled="isBuilding"
                                            class="w-full bg-gradient-to-r from-purple-600 to-pink-600 text-white py-4 rounded-xl font-semibold hover:from-purple-700 hover:to-pink-700 disabled:from-gray-400 disabled:to-gray-500 transition-all duration-300 hover-lift shadow-lg">
                                        <i class="fas fa-play mr-2"></i>加载演示规则
                                    </button>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 规则预览 -->
                        <div v-if="currentRuleSet" class="mt-8 bg-white/90 backdrop-blur-sm rounded-2xl shadow-2xl p-6 hover-lift">
                            <div class="flex items-center mb-4">
                                <div class="bg-gradient-to-r from-blue-600 to-purple-600 p-2 rounded-lg text-white">
                                    <i class="fas fa-list-alt"></i>
                                </div>
                                <h3 class="text-xl font-bold text-gray-900 ml-3">规则预览</h3>
                                <span class="ml-auto text-sm text-gray-500">{{ currentRuleSet.total_rules }} 条规则</span>
                            </div>
                            
                            <div class="space-y-3 max-h-96 overflow-y-auto">
                                <div v-for="rule in currentRuleSet.rules.slice(0, 5)" :key="rule.rule_id"
                                     :class="['rule-card', getSeverityClass(rule.severity)]"
                                     class="p-4 rounded-xl border">
                                    <div class="flex items-start justify-between">
                                        <div class="flex-1">
                                            <h4 class="font-semibold text-gray-900">{{ rule.title }}</h4>
                                            <p class="text-sm text-gray-600 mt-1">{{ rule.description }}</p>
                                            <div class="flex items-center mt-2 space-x-2">
                                                <span class="text-xs bg-gray-100 text-gray-700 px-2 py-1 rounded">{{ rule.category }}</span>
                                                <span :class="getSeverityBadge(rule.severity)" class="text-xs px-2 py-1 rounded font-medium">
                                                    {{ rule.severity }}
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div v-if="currentRuleSet.rules.length > 5" class="text-center text-sm text-gray-500">
                                    还有 {{ currentRuleSet.rules.length - 5 }} 条规则...
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 统计信息 -->
                    <div>
                        <div class="bg-white/90 backdrop-blur-sm rounded-2xl shadow-2xl p-6 hover-lift">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">构建统计</h3>
                            <div class="space-y-4">
                                <div class="flex justify-between items-center">
                                    <span class="text-gray-600">已构建规则集</span>
                                    <span class="font-semibold text-blue-600">{{ stats.totalRuleSets }}</span>
                                </div>
                                <div class="flex justify-between items-center">
                                    <span class="text-gray-600">总规则数量</span>
                                    <span class="font-semibold text-green-600">{{ stats.totalRules }}</span>
                                </div>
                                <div class="flex justify-between items-center">
                                    <span class="text-gray-600">最近更新</span>
                                    <span class="text-sm text-gray-500">{{ stats.lastUpdate }}</span>
                                </div>
                            </div>
                        </div>
                        
                        <div class="mt-6 bg-gradient-to-r from-green-50 to-blue-50 rounded-2xl shadow-2xl p-6 hover-lift">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">使用提示</h3>
                            <div class="space-y-3 text-sm text-gray-600">
                                <div class="flex items-start">
                                    <i class="fas fa-lightbulb text-yellow-500 mt-0.5 mr-2"></i>
                                    <span>选择合适的建筑类型以获得更精准的规则</span>
                                </div>
                                <div class="flex items-start">
                                    <i class="fas fa-cog text-blue-500 mt-0.5 mr-2"></i>
                                    <span>填写项目上下文可优化规则参数</span>
                                </div>
                                <div class="flex items-start">
                                    <i class="fas fa-history text-green-500 mt-0.5 mr-2"></i>
                                    <span>所有构建的规则都会自动保存到历史记录</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 历史记录页面 -->
            <div v-if="currentTab === 'history'" class="space-y-6">
                <div class="bg-white/90 backdrop-blur-sm rounded-2xl shadow-2xl p-6 hover-lift">
                    <div class="flex items-center justify-between mb-6">
                        <h2 class="text-2xl font-bold text-gray-900">规则构建历史</h2>
                        <button @click="refreshHistory" 
                                class="bg-gradient-to-r from-blue-600 to-purple-600 text-white px-4 py-2 rounded-lg hover:from-blue-700 hover:to-purple-700 transition-all duration-300">
                            <i class="fas fa-sync-alt mr-2"></i>刷新
                        </button>
                    </div>
                    
                    <div v-if="ruleHistory.length === 0" class="text-center py-12">
                        <div class="bg-gray-100 w-24 h-24 rounded-full flex items-center justify-center mx-auto mb-4">
                            <i class="fas fa-history text-3xl text-gray-400"></i>
                        </div>
                        <h3 class="text-lg font-semibold text-gray-900 mb-2">暂无构建历史</h3>
                        <p class="text-gray-600">开始构建您的第一个AI规则库吧！</p>
                    </div>
                    
                    <div v-else class="grid gap-4">
                        <div v-for="ruleSet in ruleHistory" :key="ruleSet.rule_set_id"
                             class="bg-white rounded-xl shadow-lg p-6 hover-lift border-l-4"
                             :class="getRuleSetBorder(ruleSet.building_type)">
                            <div class="flex items-start justify-between">
                                <div class="flex-1">
                                    <div class="flex items-center space-x-3 mb-2">
                                        <span class="text-lg font-semibold text-gray-900">规则集 #{{ ruleSet.rule_set_id }}</span>
                                        <span :class="getBuildingTypeBadge(ruleSet.building_type)" class="text-xs px-2 py-1 rounded-full font-medium">
                                            {{ getBuildingTypeLabel(ruleSet.building_type) }}
                                        </span>
                                    </div>
                                    <div class="flex items-center space-x-4 text-sm text-gray-600 mb-3">
                                        <span><i class="fas fa-list-ol mr-1"></i>{{ ruleSet.total_rules }} 条规则</span>
                                        <span><i class="fas fa-clock mr-1"></i>{{ formatDate(ruleSet.created_at) }}</span>
                                        <span v-if="ruleSet.metadata.context_used" class="text-green-600">
                                            <i class="fas fa-check-circle mr-1"></i>已优化
                                        </span>
                                    </div>
                                    <div class="flex flex-wrap gap-1">
                                        <span v-for="category in getCategories(ruleSet.rules)" :key="category"
                                              class="text-xs bg-gray-100 text-gray-700 px-2 py-1 rounded">
                                            {{ category }}
                                        </span>
                                    </div>
                                </div>
                                <div class="flex space-x-2">
                                    <button @click="viewRuleSet(ruleSet)" 
                                            class="bg-blue-600 text-white px-3 py-1 rounded-lg text-sm hover:bg-blue-700 transition-colors">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                    <button @click="downloadRuleSet(ruleSet)" 
                                            class="bg-green-600 text-white px-3 py-1 rounded-lg text-sm hover:bg-green-700 transition-colors">
                                        <i class="fas fa-download"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 规则预览页面 -->
            <div v-if="currentTab === 'preview'" class="space-y-6">
                <div class="bg-white/90 backdrop-blur-sm rounded-2xl shadow-2xl p-6 hover-lift">
                    <div class="flex items-center justify-between mb-6">
                        <h2 class="text-2xl font-bold text-gray-900">规则详细预览</h2>
                        <button @click="loadDemoRules" 
                                class="bg-gradient-to-r from-purple-600 to-pink-600 text-white px-4 py-2 rounded-lg hover:from-purple-700 hover:to-pink-700 transition-all duration-300">
                            <i class="fas fa-play mr-2"></i>加载演示
                        </button>
                    </div>
                    
                    <div v-if="currentRuleSet" class="space-y-6">
                        <div class="bg-gradient-to-r from-blue-50 to-purple-50 rounded-xl p-6">
                            <div class="grid md:grid-cols-4 gap-4 text-center">
                                <div>
                                    <div class="text-2xl font-bold text-blue-600">{{ currentRuleSet.total_rules }}</div>
                                    <div class="text-sm text-gray-600">总规则数</div>
                                </div>
                                <div>
                                    <div class="text-2xl font-bold text-red-600">{{ getSeverityCount('严重') }}</div>
                                    <div class="text-sm text-gray-600">严重级别</div>
                                </div>
                                <div>
                                    <div class="text-2xl font-bold text-yellow-600">{{ getSeverityCount('中等') }}</div>
                                    <div class="text-sm text-gray-600">中等级别</div>
                                </div>
                                <div>
                                    <div class="text-2xl font-bold text-green-600">{{ getSeverityCount('轻微') }}</div>
                                    <div class="text-sm text-gray-600">轻微级别</div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="space-y-4">
                            <div v-for="rule in currentRuleSet.rules" :key="rule.rule_id"
                                 :class="['rule-card', getSeverityClass(rule.severity)]"
                                 class="bg-white rounded-xl shadow-lg p-6 border-l-4">
                                <div class="grid md:grid-cols-12 gap-6">
                                    <div class="md:col-span-8">
                                        <div class="flex items-start space-x-3">
                                            <div :class="getSeverityIcon(rule.severity)" class="text-white p-2 rounded-full text-sm">
                                                <i :class="getSeverityIconClass(rule.severity)"></i>
                                            </div>
                                            <div>
                                                <h3 class="text-lg font-semibold text-gray-900">{{ rule.title }}</h3>
                                                <p class="text-gray-600 mt-1">{{ rule.description }}</p>
                                                <div class="flex items-center space-x-3 mt-2 text-sm">
                                                    <span class="bg-gray-100 text-gray-700 px-2 py-1 rounded">{{ rule.category }}</span>
                                                    <span class="text-gray-500">{{ rule.standard }}</span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="md:col-span-4">
                                        <div class="space-y-2 text-sm">
                                            <div class="flex justify-between">
                                                <span class="text-gray-600">检查条件:</span>
                                                <span class="font-mono text-xs bg-gray-100 px-2 py-1 rounded">{{ rule.condition }}</span>
                                            </div>
                                            <div class="flex justify-between">
                                                <span class="text-gray-600">阈值:</span>
                                                <span class="font-semibold">{{ rule.threshold }} {{ rule.unit }}</span>
                                            </div>
                                            <div class="flex justify-between">
                                                <span class="text-gray-600">严重级别:</span>
                                                <span :class="getSeverityText(rule.severity)" class="font-semibold">{{ rule.severity }}</span>
                                            </div>
                                        </div>
                                        <div class="mt-3 p-3 bg-yellow-50 rounded-lg">
                                            <div class="text-xs font-medium text-yellow-800">建议整改:</div>
                                            <div class="text-xs text-yellow-700 mt-1">{{ rule.suggested_fix }}</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div v-else class="text-center py-12">
                        <div class="bg-gray-100 w-24 h-24 rounded-full flex items-center justify-center mx-auto mb-4">
                            <i class="fas fa-eye text-3xl text-gray-400"></i>
                        </div>
                        <h3 class="text-lg font-semibold text-gray-900 mb-2">暂无规则预览</h3>
                        <p class="text-gray-600">请先构建规则库或加载演示规则</p>
                    </div>
                </div>
            </div>
        </main>

        <!-- 规则详情模态框 -->
        <div v-if="showRuleModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
            <div class="bg-white rounded-2xl shadow-2xl max-w-4xl w-full mx-4 max-h-[80vh] overflow-y-auto">
                <div class="p-6 border-b">
                    <div class="flex items-center justify-between">
                        <h3 class="text-xl font-bold text-gray-900">规则详情</h3>
                        <button @click="showRuleModal = false" class="text-gray-500 hover:text-gray-700">
                            <i class="fas fa-times text-xl"></i>
                        </button>
                    </div>
                </div>
                <div class="p-6">
                    <div v-if="selectedRuleSet" class="space-y-4">
                        <div v-for="rule in selectedRuleSet.rules" :key="rule.rule_id" class="border rounded-lg p-4">
                            <h4 class="font-semibold text-gray-900">{{ rule.title }}</h4>
                            <p class="text-gray-600 text-sm mt-1">{{ rule.description }}</p>
                            <div class="mt-2 text-sm space-y-1">
                                <div><strong>类别:</strong> {{ rule.category }}</div>
                                <div><strong>标准:</strong> {{ rule.standard }}</div>
                                <div><strong>条件:</strong> <code class="bg-gray-100 px-2 py-1 rounded">{{ rule.condition }}</code></div>
                                <div><strong>阈值:</strong> {{ rule.threshold }} {{ rule.unit }}</div>
                                <div><strong>严重级别:</strong> <span :class="getSeverityText(rule.severity)">{{ rule.severity }}</span></div>
                                <div><strong>建议:</strong> {{ rule.suggested_fix }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        const { createApp } = Vue;

        createApp({
            data() {
                return {
                    currentTab: 'build',
                    selectedBuildingType: '',
                    projectContext: {
                        building_height: '',
                        total_area: '',
                        occupancy: 'standard'
                    },
                    isBuilding: false,
                    currentRuleSet: null,
                    ruleHistory: [],
                    selectedRuleSet: null,
                    showRuleModal: false,
                    stats: {
                        totalRuleSets: 0,
                        totalRules: 0,
                        lastUpdate: '暂无'
                    },
                    buildingTypes: [
                        { value: 'residential', label: '住宅建筑', icon: 'fas fa-home' },
                        { value: 'commercial', label: '商业建筑', icon: 'fas fa-building' },
                        { value: 'industrial', label: '工业建筑', icon: 'fas fa-industry' }
                    ]
                };
            },
            mounted() {
                this.loadStats();
                this.loadHistory();
            },
            methods: {
                async buildAIRules() {
                    if (!this.selectedBuildingType) return;
                    
                    this.isBuilding = true;
                    try {
                        const context = {};
                        if (this.projectContext.building_height) context.building_height = parseFloat(this.projectContext.building_height);
                        if (this.projectContext.total_area) context.total_area = parseFloat(this.projectContext.total_area);
                        if (this.projectContext.occupancy) context.occupancy = this.projectContext.occupancy;
                        
                        const response = await axios.post('/api/ai-rules/build', {
                            building_type: this.selectedBuildingType,
                            context: Object.keys(context).length > 0 ? context : null
                        });
                        
                        this.currentRuleSet = response.data;
                        await this.loadStats();
                        await this.loadHistory();
                        
                        // 自动切换到预览标签
                        this.currentTab = 'preview';
                    } catch (error) {
                        console.error('构建规则失败:', error);
                        alert('构建规则失败: ' + (error.response?.data?.detail || error.message));
                    } finally {
                        this.isBuilding = false;
                    }
                },
                
                async loadDemoRules() {
                    try {
                        const response = await axios.get('/api/ai-rules/demo');
                        this.currentRuleSet = response.data;
                        this.currentTab = 'preview';
                    } catch (error) {
                        console.error('加载演示规则失败:', error);
                    }
                },
                
                async loadHistory() {
                    try {
                        const response = await axios.get('/api/ai-rules/history');
                        this.ruleHistory = response.data;
                    } catch (error) {
                        console.error('加载历史记录失败:', error);
                    }
                },
                
                async loadStats() {
                    try {
                        const response = await axios.get('/api/ai-rules/history', { params: { limit: 100 } });
                        const history = response.data;
                        this.stats.totalRuleSets = history.length;
                        this.stats.totalRules = history.reduce((sum, set) => sum + set.total_rules, 0);
                        this.stats.lastUpdate = history.length > 0 ? this.formatDate(history[0].created_at) : '暂无';
                    } catch (error) {
                        console.error('加载统计失败:', error);
                    }
                },
                
                async viewRuleSet(ruleSet) {
                    try {
                        const response = await axios.get(`/api/ai-rules/${ruleSet.rule_set_id}`);
                        this.selectedRuleSet = response.data;
                        this.showRuleModal = true;
                    } catch (error) {
                        console.error('获取规则集详情失败:', error);
                    }
                },
                
                downloadRuleSet(ruleSet) {
                    const dataStr = JSON.stringify(ruleSet, null, 2);
                    const dataBlob = new Blob([dataStr], { type: 'application/json' });
                    const url = URL.createObjectURL(dataBlob);
                    const link = document.createElement('a');
                    link.href = url;
                    link.download = `ai-rules-${ruleSet.rule_set_id}.json`;
                    link.click();
                    URL.revokeObjectURL(url);
                },
                
                getSeverityClass(severity) {
                    return {
                        '严重': 'severity-critical',
                        '中等': 'severity-medium',
                        '轻微': 'severity-minor'
                    }[severity] || '';
                },
                
                getSeverityBadge(severity) {
                    return {
                        '严重': 'bg-red-100 text-red-800',
                        '中等': 'bg-yellow-100 text-yellow-800',
                        '轻微': 'bg-green-100 text-green-800'
                    }[severity] || 'bg-gray-100 text-gray-800';
                },
                
                getSeverityText(severity) {
                    return {
                        '严重': 'text-red-600',
                        '中等': 'text-yellow-600',
                        '轻微': 'text-green-600'
                    }[severity] || 'text-gray-600';
                },
                
                getSeverityIcon(severity) {
                    return {
                        '严重': 'bg-red-500',
                        '中等': 'bg-yellow-500',
                        '轻微': 'bg-green-500'
                    }[severity] || 'bg-gray-500';
                },
                
                getSeverityIconClass(severity) {
                    return {
                        '严重': 'fas fa-exclamation-triangle',
                        '中等': 'fas fa-exclamation-circle',
                        '轻微': 'fas fa-info-circle'
                    }[severity] || 'fas fa-question-circle';
                },
                
                getBuildingTypeBadge(type) {
                    const badges = {
                        'residential': 'bg-blue-100 text-blue-800',
                        'commercial': 'bg-green-100 text-green-800',
                        'industrial': 'bg-purple-100 text-purple-800'
                    };
                    return badges[type] || 'bg-gray-100 text-gray-800';
                },
                
                getBuildingTypeLabel(type) {
                    const labels = {
                        'residential': '住宅',
                        'commercial': '商业',
                        'industrial': '工业'
                    };
                    return labels[type] || type;
                },
                
                getRuleSetBorder(type) {
                    const borders = {
                        'residential': 'border-blue-500',
                        'commercial': 'border-green-500',
                        'industrial': 'border-purple-500'
                    };
                    return borders[type] || 'border-gray-500';
                },
                
                getCategories(rules) {
                    const categories = [...new Set(rules.map(rule => rule.category))];
                    return categories;
                },
                
                getSeverityCount(severity) {
                    if (!this.currentRuleSet) return 0;
                    return this.currentRuleSet.rules.filter(rule => rule.severity === severity).length;
                },
                
                formatDate(dateString) {
                    return new Date(dateString).toLocaleString('zh-CN', {
                        year: 'numeric',
                        month: '2-digit',
                        day: '2-digit',
                        hour: '2-digit',
                        minute: '2-digit'
                    });
                },
                
                refreshHistory() {
                    this.loadHistory();
                    this.loadStats();
                }
            }
        }).mount('#app');
    </script>
</body>
</html>